{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3e95a51a",
   "metadata": {},
   "source": [
    "-------------\n",
    "* 记录人：廖洪恩\n",
    "* 时间：2021年11月29日\n",
    "* 内容：字典的集合和数据结构综合实践\n",
    "* 主要参考文档：\n",
    "> 1.python文档：https://www.python.org/\n",
    "> 2.志超博客：https://www.cnblogs.com/xuzhichao/p/11570285.html"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "561e580f",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "\n",
    "> 1.函数的定义\n",
    "\n",
    "> 2.如何使用函数\n",
    "\n",
    "> 3.函数的返回值\n",
    "\n",
    "> 4.使用注释改进文档\n",
    "\n",
    "> 5.传递任意数量的实参\n",
    "\n",
    "> 6.函数与模块\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87b35607",
   "metadata": {},
   "source": [
    "## 函数的定义\n",
    "--------------------------------\n",
    "> 1.写def，def 定义函数的第一步\n",
    "\n",
    "> 2.写定义函数的名称，def function_one\n",
    "\n",
    ">> 1.function_name遵循变量名称定义的规则\n",
    "\n",
    "> 3.函数名称后加上“（）：”，def function_name（）：===》f（x）=2x\n",
    "\n",
    "> 4.代码块：\n",
    "def fib(n):\n",
    "    \"\"\"Print a Fibonacci series up to n.\"\"\"\n",
    "    a,b = 0,1\n",
    "    while a < n:\n",
    "        print(a,end='')\n",
    "        a,b = b,a+b\n",
    "        print()\n",
    "---------------------------------\n",
    "\n",
    "* 书写函数体\n",
    "> 1.函数的注释 \n",
    "> 2.函数的具体操作（函数体）\n",
    "> 3.注意：函数需要有tab键的间距\n",
    "\n",
    "* 使用函数（函数的调用）\n",
    "> 1.function_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8d6c67a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib(n):\n",
    "    \"\"\"Print a Fibonacci series up to n.\"\"\"\n",
    "    a,b = 0,1\n",
    "    while a < n:\n",
    "        print(a,end='')\n",
    "        a,b = b,a+b\n",
    "        print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8f5d54e2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "1\n",
      "2\n",
      "3\n",
      "5\n",
      "8\n",
      "13\n",
      "21\n",
      "34\n",
      "55\n",
      "89\n"
     ]
    }
   ],
   "source": [
    "fib(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "764491e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function fib in module __main__:\n",
      "\n",
      "fib(n)\n",
      "    Print a Fibonacci series up to n.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(fib)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4ec4f786",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 尝试写实现一个hello world的函数\n",
    "def hello():\n",
    "    \"\"\"打印hello world\"\"\"\n",
    "    print(\"hello world！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "fb482184",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c3b603d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数存在的意义\n",
    "## 1、可以调用函数的方式来实现某些功能，使用人不需要考虑代码函数内容，节省时间，应用效率\n",
    "## 2、函数定义可重复使用\n",
    "# 课本实践149页"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "da27db7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(word):\n",
    "    \"\"\"打印文本中出现的元音字母,请输入要查询的文本内容\"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(word))\n",
    "    print(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "fc29d2ee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function search4vowels in module __main__:\n",
      "\n",
      "search4vowels(word)\n",
      "    打印文本中出现的元音字母,请输入要查询的文本内容\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(search4vowels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "4cf42c63",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'e', 'i', 'o'}\n"
     ]
    }
   ],
   "source": [
    "search4vowels(\"hdsifjeop\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbd9d486",
   "metadata": {},
   "source": [
    "--------\n",
    "* 代码重复，且没有特殊的定义（如：注释，名称）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b34b07e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "vowels = set('aeiou')\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = vowels.intersection(set(word))\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acc55692",
   "metadata": {},
   "outputs": [],
   "source": [
    "vowels = set('aeiou')\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = vowels.intersection(set(word))\n",
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "686bd406",
   "metadata": {},
   "source": [
    "## 函数的参数\n",
    "\n",
    "* <font style=\"color:red>注意：函数参数可以不写，如果有，则如下（分实际参数和形式参数）：</font>\n",
    "    \n",
    "> 1.形式参数\n",
    "    \n",
    ">> 1.在定义过程中，函数的参数为形式参数\n",
    "    \n",
    "> 2.实际参数\n",
    "    \n",
    ">> 2.调用函数的过程中，函数的参数为实际参数\n",
    "    \n",
    ">> 2.调用函数时，形式参数被分配空间，成为实际参数并应用过来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "1130811e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def greet_user():    #定义一个函数,关键词为\"def\",函数名为\"greet_user\",最后以“：”结尾\n",
    "    \"\"\"显示简单的问候语\"\"\"    #描述函数的具体功能\n",
    "    print(\"Hello,\"+username.title()+\"!\")         #函数体的代码块，用于实现函数功能\n",
    "    greet_user()         #调用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "715cdfbd",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "greet_user() takes 0 positional arguments but 1 was given",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-38-96291c99d27b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 调用函数 username=“hongen”\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mgreet_user\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"hongen\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: greet_user() takes 0 positional arguments but 1 was given"
     ]
    }
   ],
   "source": [
    "# 调用函数 username=“hongen”\n",
    "greet_user(\"hongen\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1b417e7",
   "metadata": {},
   "source": [
    "* 当参数不止一个\n",
    "> 1.位置参数( positional argument)\n",
    "\n",
    ">> 1.形式参数有多个值，如果实际参数直接赋值，按照形式参数的先后顺序，方式称：位置参数赋值。在调用函数过程中（考虑位置，考虑个数）\n",
    "\n",
    "> 2.关键字参数[https://docs.python.org/3/tutorial/controlflow.html#keyword-arguments]\n",
    "\n",
    ">> 1.在调用函数时，我们直接在实际参数中去用赋值的方法定义形式参数\n",
    "\n",
    ">> 2.不需要考虑顺序，因为赋值已经确实能够了形式参数的值\n",
    "\n",
    "> 3.默认参数\n",
    "\n",
    ">> 1.默认参数，在形式参数中（预计有大量的调用会使用的值，可以预先预定）\n",
    "\n",
    ">> 2.默认参数存在的话,实际调用过程中可以省略赋值的过程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "5fed965d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#zhichao 请注意，有两个参数\n",
    "\n",
    "def describe_pet(animal_type,pet_name):\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a\" + animal_type + \".\")\n",
    "    print(\"My\" + animal_type + \"'s name is \"+pet_name.title() + \".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "da466034",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "describe_pet() missing 1 required positional argument: 'pet_name'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-41-a70f5cbb422b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdescribe_pet\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"dog\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: describe_pet() missing 1 required positional argument: 'pet_name'"
     ]
    }
   ],
   "source": [
    "describe_pet(\"dog\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "583b94a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have adog.\n",
      "Mydog's name is 豆豆.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(animal_type=\"dog\",pet_name=\"豆豆\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "789ce4ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have adog.\n",
      "Mydog's name is 豆豆.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(animal_type=\"dog\",pet_name=\"豆豆\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "ccacec63",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have adog.\n",
      "Mydog's name is 豆豆.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(pet_name=\"豆豆\",animal_type=\"dog\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "03b7c7c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 默认参数是写在形式参数中的\n",
    "def describe_pet(pet_name,animal_type=\"dog\"):\n",
    "    \"\"\"显示宠物信息，animal_type的默认值'dog'\"\"\"\n",
    "    print(\"\\nI have a\" + animal_type + \".\")\n",
    "    print(\"My\" + animal_type + \"'s name is \"+pet_name.title() + \".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "90c3ee78",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have adog.\n",
      "Mydog's name is 豆豆.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"豆豆\",\"dog\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a1df976",
   "metadata": {},
   "source": [
    "---------\n",
    "\n",
    "* 实操与注意\n",
    "\n",
    "> 1.如果想同时使用位置参数和关键字参数\n",
    "\n",
    ">> 1.（位置参数需要放在关键字参数前面）\n",
    "\n",
    "> 2.默认参数\n",
    "\n",
    ">> 1.非默认参数需要放在默认参数前面\n",
    "\n",
    ">> 2.默认参数放在末尾（均是关键字参数，不考虑顺序）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "7c46eac4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "#eg1\n",
    "def func_test(x,y=3):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    \n",
    "func_test(1,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "36222a9c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "1\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "#eg1\n",
    "def func_test(x,y,z):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)\n",
    "    \n",
    "func_test(2,1,z=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db652423",
   "metadata": {},
   "source": [
    "## 课后浏览第四章函数与模块的课本内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37798252",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
